package bulid;

import java.util.concurrent.*;

/**
 * @author aiowang
 * @description:
 * @date 2020/9/10 21:44
 */
public class ExecutorServiceTest {

    public static void main(String[] args) {
        // 固定线程池
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);

        // ExecutorService 执行 实现Runable接口的线程 任务
        fixedThreadPool.execute(new Runnable(){
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()
                        + "ExecutorService execute Runnable -- 线程4");
            }

        });

        // ExecutorService 提交 实现Runable接口的线程 任务
        Future submit = fixedThreadPool.submit(new Runnable(){
            public void run() {
                System.out.println(Thread.currentThread().getName()
                        + "ExecutorService submit Runnable -- 线程4");
            }
        });
        try {
            // Future 的get方法 ： 等待计算完成，然后检索其结果
            submit.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        // ExecutorService 提交 实现Callable接口的线程 任务
        Future<String> submit2 = fixedThreadPool.submit(new Callable(){

            @Override
            public String call() throws Exception {
                return Thread.currentThread().getName()
                        + "ExecutorService submit Callable -- 线程5";
            }

        });
        try {
            String str = submit2.get();
            System.out.println(str);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        // 可缓存线程池
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

        // 单工作线程
        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();

        // 支持延迟，定时任务
        ScheduledExecutorService scheduledThreadPool =
                Executors.newScheduledThreadPool(5);

        // 支持延迟，定时任务的单线程
        ScheduledExecutorService singleThreadScheduledExecutor =
                Executors.newSingleThreadScheduledExecutor();
    }

}
